package cn.iocoder.yudao.module.mp.api.user;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.mp.api.user.dto.UserMpTemplateMsgSendReqDTO;
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import cn.iocoder.yudao.module.mp.dal.dataobject.user.MpUserDO;
import cn.iocoder.yudao.module.mp.enums.message.MpMessageSendTypeEnum;
import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
import cn.iocoder.yudao.module.mp.service.user.MpUserService;
import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

@Service
@Validated
public class MpUserApiImpl implements MpUserApi {

    @Resource
    private MpUserService mpUserService;

    @Resource
    private MpAccountService mpAccountService;

    @Resource
    private SocialUserApi socialUserApi;

    @Override
    public boolean getQrcode(Long userId) {
        return mpUserService.getQrcode(userId);
    }

    @Override
    public boolean sendTemplateMsg(UserMpTemplateMsgSendReqDTO reqDTO) {
        List<SocialUserRespDTO> socialUserList = socialUserApi.getSocialUserByUserId(reqDTO.getUserType(), reqDTO.getUserId());
        if(CollectionUtils.isAnyEmpty(socialUserList)){
            return false;
        }

        boolean flag = false;
        for(SocialUserRespDTO socialUser : socialUserList){
            if(SocialTypeEnum.WECHAT_MINI_APP.getType().equals(socialUser.getType())){
                flag = sendWechatMiniApp(reqDTO, socialUser);
            }else if(SocialTypeEnum.WECHAT_MP.getType().equals(socialUser.getType())){
                flag = sendWechatMp(reqDTO, socialUser);
            }
            if(flag){
                return true;
            }
        }
        return false;
    }

    private boolean sendWechatMiniApp(UserMpTemplateMsgSendReqDTO reqDTO, SocialUserRespDTO socialUser) {
        reqDTO.setAppId(socialUser.getClientId());

        if(StringUtils.isBlank(socialUser.getUnionId())){
            return false;
        }

        MpUserDO mpUserDO = mpUserService.selectByUnionId(socialUser.getUnionId());
        if(mpUserDO == null){
            return false;
        }
        // 构建保存的 MpUserDO 对象
        MpAccountDO account = mpAccountService.getAccountFromCache(mpUserDO.getAppId());
        if(account == null){
            return false;
        }

        String tpl = getTpl(account, reqDTO.getMsgType());

        if(StringUtils.isBlank(tpl)){
            return false;
        }

        return mpUserService.sendTemplateMsg(reqDTO, mpUserDO.getAppId(), mpUserDO.getOpenid(), tpl);
    }

    private boolean sendWechatMp(UserMpTemplateMsgSendReqDTO reqDTO, SocialUserRespDTO socialUser) {
        // 构建保存的 MpUserDO 对象
        MpAccountDO account = mpAccountService.getAccountFromCache(socialUser.getClientId());
        if(account == null){
            return false;
        }

        String tpl = getTpl(account, reqDTO.getMsgType());

        // 组装url
        String url = account.getDomain()+"#"+reqDTO.getPage();
        reqDTO.setUrl(url);

        if(StringUtils.isBlank(tpl)){
            return false;
        }

        return mpUserService.sendTemplateMsg(reqDTO, socialUser.getClientId(), socialUser.getOpenid(), tpl);
    }

    /**
     * 获取模板
     * @param account
     * @param msgType
     * @return
     */
    private String getTpl(MpAccountDO account, Integer msgType) {
        String tpl = null;
        // TODO 扩展模板消息
        if(MpMessageSendTypeEnum.PAY_SUCCESS.getType().equals(msgType)){
            tpl = account.getTplAdd();
        }else if(MpMessageSendTypeEnum.UNREAD_MSG.getType().equals(msgType)){
            tpl = account.getTemplateId();
        }else if(MpMessageSendTypeEnum.ORDER_SUCCESS.getType().equals(msgType)){
            tpl = account.getTplComment();
        }else if(MpMessageSendTypeEnum.DELIVERY_SUCCESS.getType().equals(msgType)){
            tpl = account.getTplStart();
        }else if(MpMessageSendTypeEnum.AFTER_SALE_ORDER.getType().equals(msgType)){
            tpl = account.getTplCancel();
        }else if(MpMessageSendTypeEnum.RECEIVE_ORDER.getType().equals(msgType)){
            tpl = account.getTplEnd();
        }
        return tpl;
    }

}
